package fr.pud.client.view;

import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;

import fr.pud.client.model.PUDStation;
import fr.pud.client.model.SuggestionAddresses;
import fr.pud.client.view.mapstation.MapStation;

/**
 * Apr 9, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         MapSuggestionAddresses.java
 *         This class represents a list of addresses suggested by Nominatim.
 *         Those addresses will be draw on the map but they have to
 *         be different that addresses of route (a different marker).
 */
public class MapSuggestionAddresses implements Observer {
    /**
     * A list of coordinates which represents all the points to draw
     */
    private static HashSet<MapStation>    suggestions;
    /**
     * A single instance of MapSuggestionAdresses
     */
    private static MapSuggestionAddresses singleton;

    public static MapSuggestionAddresses getMapSuggestionAddresses() {
        initialize();
        return singleton;
    }

    private static void initialize() {
        if (singleton == null) {
            singleton = new MapSuggestionAddresses();
        }
    }

    public static void setMapSuggestionAddresses() {
        for (PUDStation pud : SuggestionAddresses.getSuggestionPUD()) {
            suggestions
                    .add(new MapStation(pud, null, MapStation.PURPLE_POINTER));
        }
    }

    /**
     * The boolean which specified if the suggestions addresses are visible.
     */
    private boolean isVisible = true;

    private MapSuggestionAddresses() {
        suggestions = new HashSet<MapStation>();
        SuggestionAddresses.getSuggestionAddresses().addObserver(this);
    }

    public HashSet<MapStation> getSuggestions() {
        initialize();
        return suggestions;
    }

    /**
     * @return true if the suggestions addresses are visible.
     */
    public boolean isVisible() {
        return this.isVisible;
    }

    /**
     * Change the visibility of the map suggestions addresses.
     * @param isVisible
     */
    public void setVisible(boolean isVisible) {
        JMapViewerExtended.getJMapViewer().setMapSuggestionVisible(isVisible);
        this.isVisible = isVisible;
    }

    /*
     * (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o.equals(SuggestionAddresses.getSuggestionAddresses())) {
            MapSuggestionAddresses.setMapSuggestionAddresses();
        }
    }
}
